// pages/vaccine/vaccine.js
const app = getApp()
import {  getpdf,getPrenatalCare } from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text1:"疾病名称",
    entext1:"Diseases",
    setMealName:"宝宝预防接种全面套餐",
    setMealenName:"Comprehensive Immunization Package",
    showdiseases:false,
    diseases:[],
    package:[],
    text3:"重要提示 Important Information",
    entext3:"Important Information",
    openinput:false,
    language:"zh",
    confirm:"确认",
    inputtext: [], 
    showinputtext:'', 
    language:"zh",
    showtext:"",
    checkinfo:"",
    information:[],
    price:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    // console.log(options)
    getPrenatalCare({selectedserver:"疫苗",selectedsetMealIndex:options.selectedsetMealIndex}).then((res)=>{
      // console.log(res.data)
      let tempprice = 0
      res.data.package.forEach(e=>{
        tempprice = tempprice + e.price
        e.check = true
      })
      // let arr = res.data.infomation.map((e,index) =>{return String(index+1) + "." +e.zh})
      // // res.data.infomation.forEach(e=>{
      //   let str = arr.join("\n")
      let str = res.data.checkinfo
      let arr = []
      res.data.infomation.forEach(e=>{
        let temp = {zh:e.zh,en:e.en}
        arr.push(temp)
      })
      if(app.globalData.language == "zh"){
        that.setData({
          language:app.globalData.language,
          setMealName:"宝宝预防接种全面套餐",
          setMealenName:"Comprehensive Immunization Package",
          text1:"疾病名称",
          checkinfo:res.data.checkinfo,
          infomations:options,
          information:arr

        })
      }else if(app.globalData.language == "en"){
        that.setData({
          language:app.globalData.language,
          setMealName:"Vaccine Package",
          text1:"Diseases",
          text3:"Important Information",
          infomations:options})
      }
      that.setData({
        price:tempprice,
        diseases:res.data.diseases,
        package:res.data.package,
        textareastr:str,
        showtext:str,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showdiseases(e){
    this.setData({showdiseases:true})
  },
  hidediseases(e){
    this.setData({showdiseases:false})
  },
  more(e){
    this.data.package[e.currentTarget.dataset.index].check = true
    this.setData({
      package:this.data.package
    })
  },
  hide(e){
    // this.setData({selectedIndex:-1})
    this.data.package[e.currentTarget.dataset.index].check = false
    this.setData({
      package:this.data.package
    })
  },
  inputprompt(e){
    let tempstr = ""
    let length  = this.data.inputtext.length
    if(this.data.inputtext.length==0){
      let  serial = this.data.information.length+1
      tempstr =  serial + '.'
    }else{
      this.data.inputtext.forEach((e,index)=>{
          let  serial = this.data.information.length+index+1
          if(index!=(length-1)){
            tempstr = tempstr + serial + '.' + e + '\n'
          }else{
            tempstr = tempstr + serial + '.' + e
          }
      })
    }

    this.setData({ openinput: true,showinputtext:tempstr })
  },
  dismiss(e){
    this.setData({openinput:false})
  },
  inputstr(e){
    console.log(e.detail.value)
    this.setData({inputvalue:e.detail.value})
  },
  confirm(e){
    this.setData({openinput:false})
  },
  prompt(e){
    let temparr = e.detail.value.split('\n')
    let temparr1 = []
    temparr.forEach(e=>{
      // if(e!=""){
        let tempstr = e.split('.').slice(1).join()
        if(tempstr != ""){
          temparr1.push(tempstr)
        }else{
          temparr1.push("")
        }
      // }
    })
    this.setData({ inputtext: temparr1})
  },
  getpdf(e){
    console.log(app.globalData.staffId)
    let data = this.data.infomations
    data.staff = app.globalData.staffId
    data.inputtext = this.data.inputtext
    data.price = this.data.price
    getpdf(data).then((res) => {
      console.log(res.data)
      wx.downloadFile({
        url: res.data,
        success (res) {
          // console.log(res)
          wx.openDocument({
            filePath: res.tempFilePath,
          })
        }
      })
    })
  },
  linechange(e){
    const that = this
    // console.log(e)
    // console.log(this.data.inputtext)
    if(this.data.inputtext.length!=0){
      let tempshowtext = ""
      let length  = this.data.inputtext.length
      this.data.inputtext.forEach((e,index)=>{
        let  serial = this.data.information.length+index+1
        if(index!=(length-1)){
          tempshowtext = tempshowtext + serial + '.' + e + '\n'
        }else{
          tempshowtext = tempshowtext + serial + '.' + e
        }
      })
      this.setData({showinputtext:tempshowtext})
    }
  }
})